const {
  getReqData,
  formatArticle
} = require('../../utils/util.js')
const {
  updateDetailExamples,
  clickLikes,
  reqCollection
} = require('../../utils/api.js')
const app = getApp()

Page({
  data: {
    hiddenFlag: true,
    collection: false
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: async function (options) {
    const eventChannel = this.getOpenerEventChannel()
    const that = this

    wx.showLoading({
      title: '请稍后...',
    })

    // 监听acceptDataFromOpenerPage事件，获取上一页面通过eventChannel传送到当前页面的数据
    eventChannel.on('acceptDataFromIndexPage', async function (data) {
      const {
        _id,
        viewingCount,
        lastTimeFormat,
        collection
      } = data

      // 根据id，读取留言信息，添加阅读数。
      // 返回的数据冲包括已经填充好的字段 messages
      const {
        result
      } = await updateDetailExamples({
        _id,
        viewingCount: (viewingCount || 0) + 1
      })
      let dataTemp = getReqData(result)

      if (!dataTemp) {
        wx.showToast({
          title: result.data.msg,
          duration: 1000
        })
        return null;
      }

      let dataResult = dataTemp

      if (lastTimeFormat !== undefined)
        dataResult.time = lastTimeFormat

      // 转url为临时下载的url
      if (dataResult.article !== undefined)
        dataResult.article = await formatArticle(dataResult.article)

      that.setData({
        ...dataResult
      })

      wx.hideLoading()
    })
  },
  /**
   * 文章的点赞前验证，并且收藏用户信息
   */
  getUserInfoClickLike: function (e) {
    const {
      userInfo
    } = e.detail
    app.globalData.userInfo = userInfo
  },
  /**
   * 文章的点赞
   */
  onClickLikes: async function () {
    const {
      _id
    } = this.data

    const {
      result
    } = await clickLikes({
      _articleId: _id
    })

    const data = getReqData(result)

    this.setData({
      ...data
    })
  },
  /**
   * 文章的收藏前验证，并且收藏用户信息
   */
  getUserInfoCollection: function (e) {
    const {
      userInfo
    } = e.detail
    app.globalData.userInfo = userInfo
  },
  /**
   * 收藏
   * 这里做到，要记录收藏者的userInfo
   */
  onCollect: async function () {
    let {
      _id,
      collection: flag,
    } = this.data

    const {
      userInfo
    } = app.globalData

    const {
      result
    } = await reqCollection({
      _id,
      flag: !flag,
      userInfo
    })

    const data = getReqData(result)
    if(!data){
      console.log(result.data)
      return null;
    }

    this.setData({
      collection: !flag
    })

    wx.showToast({
      title: !flag ? '已收藏' : '取消收藏',
    })
  },
  /**
   * 分享
   */
  onClickShare: function () {
    wx.showShareMenu({
      withShareTicket: true
    })
  },
  /**
   * 用来展开/关闭留言板
   */
  onClickMessage: function (e) {
    let {
      hiddenFlag
    } = this.data;
    this.setData({
      hiddenFlag: !hiddenFlag
    })
  },
  /**
   * 提交信息
   */
  blogFormSubmit: async function (e) {
    let {
      _id,
      messages
    } = this.data

    const {
      textarea
    } = e.detail.value

    this.setData({
      hiddenFlag: true
    })

    const {
      detail
    } = getApp().globalData

    if (!messages) {
      messages = {}
      messages.arr = []
    }
    const arrMessagesUpdate = {
      content: textarea,
      detail: detail,
      status: 1
    }

    const {
      result
    } = await updateDetailExamples({
      _id,
      messages: arrMessagesUpdate
    })
    const data = getReqData(result)
    if (!data) return null

    this.setData({
      messages: data
    })
  },
  /**
   * 评论的点赞
   */
  onClickMessageLikes: function (e) {
    console.log('评论的点赞', e)
    const {
      index
    } = e.currentTarget.dataset
  },
})